﻿using System.Linq;
using Lsk.Data;
using System.Data.Entity.Core.Objects;
using Lsk.Business.DTO;
using AugmentLib.InputOutput;
using System.Net.Mail;
using System.Net;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace Lsk.Business
{

    public class Users
    {
        private lskEntities _entity;

        public Users()
        {
            _entity = new lskEntities();

        }


        //public bool isUser(string email, string password)
        //{
        //    ObjectParameter resultType = new ObjectParameter("ResultType", typeof(string));
        //    ObjectParameter resultMessage = new ObjectParameter("ResultMessage", typeof(string));
        //    ObjectResult<VerifyUser_Result> result = _entity.VerifyUser(email, password, resultType, resultMessage);

        //    VerifyUser_Result output = result.FirstOrDefault();
        //    string C_ResultMessage = output.C_ResultMessage;
        //    string C_ResultType = output.C_ResultType;
        //    if (resultType.Value.ToString() == "1")
        //        return true;
        //    else
        //        return false;

        //}


        public LoginResponse isUser(string email, string password)
        {
            ObjectParameter resultType = new ObjectParameter("ResultType", typeof(string));
            ObjectParameter resultMessage = new ObjectParameter("ResultMessage", typeof(string));            
            ObjectResult<VerifyUserWeb_Result> result = _entity.VerifyUserWeb(email, password, Encryption.SymmetricEncrypt(password, "augment"), resultType, resultMessage);

            VerifyUserWeb_Result output = result.FirstOrDefault();
            string C_ResultMessage = output.C_ResultMessage;
            string C_ResultType = output.C_ResultType;
            LoginResponse response = new LoginResponse();
            response.isUser = C_ResultType;
            response.UserType = C_ResultMessage;
            return response;


        }

        public LoginResponse isFbUser(string fbId)
        {
            ObjectParameter resultType = new ObjectParameter("ResultType", typeof(string));
            ObjectParameter resultMessage = new ObjectParameter("ResultMessage", typeof(string));

            ObjectResult<VerifyFbUser_Result> result = _entity.VerifyFbUser(fbId, resultType, resultMessage);

            VerifyFbUser_Result output = result.FirstOrDefault();
            string C_ResultMessage = output.C_ResultMessage;
            string C_ResultType = output.C_ResultType;
            LoginResponse response = new LoginResponse();
            response.isUser = C_ResultType;
            response.UserType = C_ResultMessage;
            return response;


        }

        public string Save(LoginInfo user)
        {
            ObjectParameter resultType = new ObjectParameter("resultType", typeof(string));
            ObjectParameter resultMessage = new ObjectParameter("resultMessage", typeof(string));
            if ((from a in _entity.Mobile_User where a.Email == user.Email select a).Count() > 0)
                return "Email account already exist!";
            ObjectResult<CreateUser_Result> result = _entity.CreateUser(user.Email, user.Handphone, user.Name, user.Password, resultType, resultMessage);
            CreateUser_Result output = result.FirstOrDefault();
            return output.C_ResultType;
        }

        public string SaveFbUser(LoginInfo user)
        {
            ObjectParameter resultType = new ObjectParameter("resultType", typeof(string));
            ObjectParameter resultMessage = new ObjectParameter("resultMessage", typeof(string));
            if ((from a in _entity.Mobile_User where a.FbId == user.FbId select a).Count() > 0)
                return "FB account already registered!";
            ObjectResult<CreateFbUser_Result> result = _entity.CreateFbUser(user.Email, user.Handphone, user.Name, user.FbId, resultType, resultMessage);
            CreateFbUser_Result output = result.FirstOrDefault();
            return output.C_ResultType;
        }

        public void SendPasswordReset(LoginInfo user)
        {
            ObjectResult<GetUserByNameEmail_Result> data = _entity.GetUserByNameEmail(user.Name, user.Email);
            GetUserByNameEmail_Result userData = data.FirstOrDefault();
            if (userData != null)
                SendEmailPasswordReset(userData.Email, userData.Name, userData.Password);
        }

        private void SendEmailPasswordReset(string To, string Name, string Password)
        {
            EmailMessage message = new EmailMessage();
            string body = @"<html><body>
                            <div style = ""background-color: #ece8d4;width: 600px; height: 200px; padding: 30px; margin - top:30px; ""><p> Dear {0},<p>          
                            <p> Please click the link below to login again.</p>
                             <a href = ""https://www.shijieproperty.com.my/Login"" > Login Here </a>
                             <br/>
                             Password: {1} <br/>                    
                            <br/>
                            <p> Thank you </p>
                           </div>
                           </body>
                           </html>";
            string messageBody = string.Format(body, Name, Password);
            message.Body = messageBody;
            message.Title = "Forget Password";
            message.Email = To;
            SendEmailMsg(message);

        }

        public IEnumerable<GetAgents_Result> GetAgentList()
        {
            ObjectResult<GetAgents_Result> agents = _entity.GetAgents();
            return agents.AsEnumerable<GetAgents_Result>();
        }

        public IEnumerable<GetAgentDetail_Result> GetAgentDetail(string staffId)
        {
            ObjectResult<GetAgentDetail_Result> agent = _entity.GetAgentDetail(staffId);
            return agent.AsEnumerable<GetAgentDetail_Result>();
        }

        public void SendEmailMsg(EmailMessage emailMsg)
        {
            string Content = emailMsg.Body;

            string smtpAddress = "mail.shijieproperty.com.my";
            int portNumber = 587;
            bool enableSSL = false;
            string emailFrom = "enquiry@shijieproperty.com.my";
            string password = "Mko)9ijN";
            string emailTo = emailMsg.Email;
            string subject = emailMsg.Title;
            string body = @Content;

            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress(emailFrom);
                mail.To.Add(emailTo);
                mail.Bcc.Add(emailFrom);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;

                using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                {
                    smtp.Credentials = new NetworkCredential(emailFrom, password);

                    smtp.EnableSsl = enableSSL;
                    // Ignore SSL error
                    ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate,
                   X509Chain chain, SslPolicyErrors sslPolicyErrors)
                    { return true; };
                    smtp.Send(mail);
                }
            }

        }

    }
}
